using System;
using System.Collections;
using System.Collections.Generic;
using MxNetwork;
using System.Threading;

public class NetworkDispatcher 
{
	public static readonly NetworkDispatcher Instance = new NetworkDispatcher();
	
	public class ChannelObs : MxNwkObsChannel
    {		
		public override bool OnConnected(int hChannel, uint LocalType, uint LocalID, uint RemoteType, uint RemoteID, string IP)
		{
            base.OnConnected(hChannel, LocalType, LocalID, RemoteType, RemoteID, IP);

            NetworkDispatcher.Instance.PushEvent(new NetworkEvent(NetworkEventType.Connect, null, 
				new ConnectEventInfo(0, hChannel, LocalType, LocalID, RemoteType, RemoteID, IP)));

			Console.WriteLine (DateTime.Now.ToString() + ": Connect Client: " + IP.ToString());

			if (NetworkDispatcher.Instance.OnConnectInfo != null)
				NetworkDispatcher.Instance.OnConnectInfo (hChannel);
                            
            return true;
        }
		
		public override bool OnDisConnected(int hChannel,int ErrorCode, uint LocalType, uint LocalID, uint RemoteType, uint RemoteID, string IP)
        {
            base.OnDisConnected(hChannel, ErrorCode, LocalType, LocalID, RemoteType, RemoteID, IP);

			NetworkDispatcher.Instance.PushEvent(new NetworkEvent(NetworkEventType.Disconnect, null, 
				new ConnectEventInfo(0, hChannel, LocalType, LocalID, RemoteType, RemoteID, IP)));

			Console.WriteLine (DateTime.Now.ToString() + ": Disconnect Client: " + IP.ToString());

			if (NetworkDispatcher.Instance.OnDisconnectInfo != null)
				NetworkDispatcher.Instance.OnDisconnectInfo (hChannel);
			
			return true;
		}
		
		public override bool OnRecvPacket(int hChl, MxNwkPacket pPkt)
		{
			NetworkDispatcher.Instance.PushEvent(new NetworkEvent(NetworkEventType.PacketRecieve, pPkt, null));
			
			return true;
		}
        
	}

	public delegate void DgtConnectInfo (int hChannel);
	public delegate void DgtDisconnectInfo (int hChannel);
	public DgtConnectInfo OnConnectInfo;
	public DgtDisconnectInfo OnDisconnectInfo;

	public delegate void DgtErrorInfo(uint ret_code, string msg);
	public DgtErrorInfo OnShowErrorInfo;
    public delegate void DgtReceiveInfo(uint ret_code, string ip);
    public DgtReceiveInfo OnShowReceiveInfo;
	
	public delegate void DgtParsePacket(MxNwkPacket pkt, MxNwkPacket pkt_rq);
	//DgtParsePacket OnParsePacket;
	
	public delegate void DgtConnectLoginConfirm(int ret_code);
	public DgtConnectLoginConfirm OnConnectLoginConfirm;
	
	public delegate void DgtDisconnectLoginConfirm();
	public DgtDisconnectLoginConfirm OnDisconnectLoginConfirm;
	
	/*
	public delegate void DgtCreateAccountConfirm(int ret_code);
	public DgtCreateAccountConfirm OnCreateAccountConfirm;
	
	public delegate void DgtValidateNameAndPasswordConfirm(int ret_code, string token, ServerInfo.GameServerInfo[] server_infos, int[] recents);
	public DgtValidateNameAndPasswordConfirm OnValidateNameAndPasswordConfirm;
	*/
		
	public delegate void DgtConnectUpdateConfirm(int ret_code);
	public DgtConnectLoginConfirm OnConnectUpdateConfirm;
	
	
	public delegate void DgtConnectGameServerConfirm(int ret_code, bool bFirstConnect);
	public DgtConnectGameServerConfirm OnConnectGameServerConfirm;
	
	public delegate void DgtDisconnectGameServerConfirm();
	public DgtDisconnectLoginConfirm OnDisconnectGameServerConfirm;
	
	/*
	public delegate void DgtValidateLoginTokenConfirm(int ret_code, bool has_charactor);
	public DgtValidateLoginTokenConfirm OnValidateLoginTokenConfirm;
	
	public delegate void DgtCreateCharactorConfirm(int ret_code);
	public DgtCreateCharactorConfirm OnCreateCharactorConfirm;
	*/
	
	
	
	enum NetworkEventType
	{
		Connect,
		
		Disconnect,
		
		PacketRecieve,
	}
	
	class ConnectEventInfo
	{
		public ConnectEventInfo(int ret, int chl, uint lt, uint lid, uint rt, uint rid, string ip)
		{
			Result = ret;
			
			HandleChannel = chl;
			
			LocalType = lt;
			
			LocalID = lid;
			
			RemoteType = rt;
			
			RemoteID = rid;
			
			IP = ip;
		}
		
		public int Result;
		
		public int HandleChannel;
		
		public uint LocalType;
		
		public uint LocalID;
		
		public uint RemoteType;
		
		public uint RemoteID;
		
		public string IP;
		
	}
	
	class NetworkEvent
	{
		public NetworkEvent(NetworkEventType event_type, MxNwkPacket pkt, ConnectEventInfo info)
		{
			Type = event_type;
			
			Packet = pkt;
			
			Info = info;
		}
		
		public NetworkEventType Type;
		
		public MxNwkPacket Packet;
		
		public ConnectEventInfo Info;
	}
	
	List<NetworkEvent> m_list_events = new List<NetworkEvent>();
	
	Dictionary<uint, DgtParsePacket> m_dct_parsers = new Dictionary<uint, DgtParsePacket>();
	
	ReaderWriterLockSlim m_lock_list_events = new ReaderWriterLockSlim();	
	
	MxNwkConnecter m_connecter_login = null;
	
	ConnectEventInfo m_connect_login_info = null;
	
	MxNwkConnecter m_connecter_game_server = null;
	
	ConnectEventInfo m_connect_game_server_info = null;
	
	void Start()
	{
		/*
		NetworkDefine.Init();
		
		m_dct_parsers.Add(NetworkDefine.Message.CreateAccount, OnParseCreateAccount);
		m_dct_parsers.Add(NetworkDefine.Message.ValidateNameAndPassword, OnParseValidateNameAndPassword);
		m_dct_parsers.Add(NetworkDefine.Message.ValidateLoginToken, OnParseValidateLoginToken);
		
		m_dct_parsers.Add(NetworkDefine.Message.CreateCharactor, OnParseCreateCharactor);
		m_dct_parsers.Add(NetworkDefine.Message.GetCharactorInfo, OnParseGetCharactorInfo);
		
		m_dct_parsers.Add(NetworkDefine.Message.GetGroupInfo, OnParseGetGroupInfo);
		m_dct_parsers.Add(NetworkDefine.Message.SetGroupMember, OnParseSetGroupMember);
		
		m_dct_parsers.Add(NetworkDefine.Message.GetAreaInfo, OnParseGetAreaInfo);
		m_dct_parsers.Add(NetworkDefine.Message.GetPlaceInfoByArea, OnParseGetPlaceInfoByArea);
		m_dct_parsers.Add(NetworkDefine.Message.GetLevelInfoByPlace, OnParseGetLevelInfoByPlace);
		
		
		OnError += OnErrorLog;*/
	}
	
	public void Update ()
	{
		DispatchEvent();
	}

	public void AddParser(uint id, DgtParsePacket pkt)
	{
		if (!m_dct_parsers.ContainsKey(id))
		{
			m_dct_parsers.Add(id, null);
		}
		
		m_dct_parsers [id] += pkt;
	}

	public void RemoveParser(uint id, DgtParsePacket pkt)
	{
		if (m_dct_parsers.ContainsKey (id))
			m_dct_parsers [id] -= pkt;
	}
	
	void PushEvent(NetworkEvent evt)
	{
		m_lock_list_events.EnterWriteLock();
		{
			m_list_events.Add(evt);
		}
		m_lock_list_events.ExitWriteLock();
	}
	
	void DispatchEvent()
	{
		List<NetworkEvent> list_events_safe = new List<NetworkEvent>();
		
		m_lock_list_events.EnterWriteLock();
		{
			foreach(NetworkEvent evt in m_list_events)
			{
				list_events_safe.Add(evt);
			}
			m_list_events.Clear();
		}
		m_lock_list_events.ExitWriteLock();
		
		
		foreach(NetworkEvent evt in list_events_safe)
		{
			switch (evt.Type)
			{
				
			case NetworkEventType.Connect:
			{
				OnEventConnectGameServer(evt.Info);
			}
			break;
			
			case NetworkEventType.Disconnect:
			{
				OnEventDisconnectGameServer();
			}
			break;
				
			case NetworkEventType.PacketRecieve:
			{
				uint result = (uint)evt.Packet.GetPacketResult();
				if (result != 0)
				{					
					if (OnShowErrorInfo != null)
					{
						string rtMsg = evt.Packet.ReadString();
						OnShowErrorInfo(result, rtMsg);
					}
				}
//				else
				{
					if (m_dct_parsers.ContainsKey(evt.Packet.GetPacketID()))
					{
                        if (OnShowReceiveInfo != null)
                        {
                            OnShowReceiveInfo(evt.Packet.GetPacketID(), evt.Packet.GetRemoteIP().GetIP());
                        }

						if ((MxNwkPacket.MxNwkPacketType)evt.Packet.GetPacketType() == MxNwkPacket.MxNwkPacketType.NPT_ACK)
						{
							MxToken token = null;
							MxNwkPacket pkt_rq = null;
							MxNetworkManager.GetInstance().RemoveSendRequest(evt.Packet.GetPacketToken(), ref pkt_rq, ref token);
							
							pkt_rq.SetReadOffset(MxNwkPacket._DataPos);
							
							if (OnRecieveRequestFromServer != null)
								OnRecieveRequestFromServer();
							
							m_dct_parsers[evt.Packet.GetPacketID()](evt.Packet, pkt_rq);							
							
						}
						else
						{
							m_dct_parsers[evt.Packet.GetPacketID()](evt.Packet, null);
						}
					}
				}
			}
			break;
				
			}
		}
	}
	
	
	
	public delegate void DgtSendRequestToServer();
	public DgtSendRequestToServer OnSendRequestToServer;
	public DgtSendRequestToServer OnRecieveRequestFromServer;
	public DgtSendRequestToServer OnReconnectToServer;
	public DgtSendRequestToServer OnReconnectToServerFinish;
	
	
	public void CreateServer(string ip, int port)
	{
		MxNwkListenerDesc desc = new MxNwkListenerDesc(ip, port, false, 1, 1);
        MxNetworkManager.GetInstance().CreateListener(desc, new MxNwkObsListener(), new ChannelObs());
	}
	
	
	MxNwkPacket reconnectRequest = null;
	public void SendRequestToServer(MxNwkPacket Req)
	{
		if (reconnectRequest != null)
			return;

		if (m_connect_game_server_info == null)
		{
			if (m_connecter_game_server == null)
			{
				reconnectRequest = Req;
				ReconnectGameServer();
			}
			
			return;
		}
		
		MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
		
		if (OnSendRequestToServer != null)
			OnSendRequestToServer();
	}
	
	public void SendMsgToServer(MxNwkPacket Req)
	{
		if (m_connect_game_server_info == null)
		{
			if (m_connecter_game_server == null)
			{
				ReconnectGameServer();
			}
			
			return;
		}
		
		MxNetworkManager.GetInstance().SendMsg(m_connect_game_server_info.HandleChannel, Req);
	}
	
	public void SendMsg(int channel, MxNwkPacket msg)
	{
		MxNetworkManager.GetInstance().SendMsg(channel, msg);
	}
	
	public void SendArk(MxNwkPacket req, MxNwkPacket ack)
	{
		MxNetworkManager.GetInstance ().SendAck (req, ack);
	}
	
	
	public void ConnectLoginServer()
	{
//		if (m_connecter_login != null || m_connect_login_info != null)
//		{
//			return;
//		}
//		
//		MxNwkConnecterDesc desc = new MxNwkConnecterDesc("192.168.1.74", 12346, false, 
//			(uint)NetworkDefine.Node.GameClient, 0, (uint)NetworkDefine.Node.LoginServer, 0);  // 连接登陆，73表示客户端，101表示登陆服务
//		
//		m_connecter_login = MxNetworkManager.GetInstance().CreateConnecter(desc, new MxNwkObsConnecter(), new ChannelObs(), false);
	}
	
	void OnEventConnectLoginServer(ConnectEventInfo info)
	{
		m_connecter_login = null;
		
		m_connect_login_info = info;
				
		if (OnConnectLoginConfirm != null)
		{
			OnConnectLoginConfirm(info.Result);
		}
	}
	
	public void DisconnectLoginServer()
	{
		if (m_connect_login_info == null)	return;
		
		MxNetworkManager.GetInstance().CloseChannel(m_connect_login_info.HandleChannel);
	}
	
	void OnEventDisconnectLoginServer()
	{
		m_connect_login_info = null;
		
		if (OnDisconnectLoginConfirm != null)
		{
			OnDisconnectLoginConfirm();
		}
	}
	
	
	/*
	public void SubmitCreateAccount(string username, string password)
	{
		if (m_connect_login_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.CreateAccount);
        Req.Write(username);
        Req.Write(password);
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_login_info.HandleChannel, Req);
		
		AccountInfo.CacheAccountInfo(username, password);
	}
	void OnParseCreateAccount(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		AccountInfo.AccountCreate();
	}
	
	
	public void SubmitValidateNameAndPassword(string username, string password)
	{
		if (m_connect_login_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        uint version = 1;
		
        Req.SetPacketID((uint)NetworkDefine.Message.ValidateNameAndPassword);
        Req.Write(username);
        Req.Write(password);
        Req.Write(version);
        MxNetworkManager.GetInstance().SendRequest(m_connect_login_info.HandleChannel, Req);
		
		AccountInfo.CacheAccountInfo(username, password);
	}
	void OnParseValidateNameAndPassword(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		string token = pkt.ReadString();
		
		AccountInfo.AccountLoginValidate(token);
		
		
		int num_server = pkt.ReadInt32();
		
		ServerInfo.GameServerInfo[] infos = new ServerInfo.GameServerInfo[num_server];
		
		for (int i = 0; i < num_server; ++ i)
		{
			infos[i] = new ServerInfo.GameServerInfo();
			
			infos[i].ID = pkt.ReadInt8();
			infos[i].Name = pkt.ReadString();
			infos[i].IP = pkt.ReadString();
			infos[i].Port = pkt.ReadInt16();
			infos[i].State = pkt.ReadInt8();
		}
		
		ServerInfo.Servers = infos;
		
		
		int num_recent = pkt.ReadInt32();
		
		int[] recents = new int[num_recent];
		
		for (int i = 0; i < num_recent; ++ i)
		{
			recents[i] = pkt.ReadInt8();
		}
		
		ServerInfo.RecentServers = recents;
	}
	*/
	
	public bool IsConnectGameServer()
	{
		return m_connect_game_server_info != null;
	}
	
	string gameServerIp = "";
	int gameServerPort = 0;
	
	public void ReconnectGameServer()
	{
		if (m_connecter_game_server != null || m_connect_game_server_info != null)
		{
			return;
		}
		
		MxNwkConnecterDesc desc = new MxNwkConnecterDesc(gameServerIp, gameServerPort, false, 1000, 0, 2000, 0);
		
		m_connecter_game_server = MxNetworkManager.GetInstance().CreateConnecter(desc, new MxNwkObsConnecter(), new ChannelObs(), false);
		
		if (OnReconnectToServer != null)
			OnReconnectToServer();
	}
	
	public void ConnectGameServer(string ip, int port)
	{
		if (m_connecter_game_server != null || m_connect_game_server_info != null)
		{
			return;
		}

		firstConnect = true;
		gameServerIp = ip;
		gameServerPort = port;
		
		MxNwkConnecterDesc desc = new MxNwkConnecterDesc(ip, port, false, 1000, 0, 2000, 0);
		
		m_connecter_game_server = MxNetworkManager.GetInstance().CreateConnecter(desc, new MxNwkObsConnecter(), new ChannelObs(), false);
	}
	
	bool firstConnect = true;
	
	void OnEventConnectGameServer(ConnectEventInfo info)
	{
		m_connecter_game_server = null;
		
		m_connect_game_server_info = info;
		
		if (!firstConnect)
		{
			if (OnReconnectToServerFinish != null)
				OnReconnectToServerFinish();
			
			if (reconnectRequest != null)
			{
				MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, reconnectRequest);
				reconnectRequest = null;
				
				if (OnSendRequestToServer != null)
					OnSendRequestToServer();
			}
		}
		
		if (firstConnect && OnConnectGameServerConfirm != null)
		{
			OnConnectGameServerConfirm(info.Result, firstConnect);
		}
		
		firstConnect = false;
	}
	
	public void DisconnectGameServer()
	{
		m_connecter_game_server = null;
		
		if (m_connect_game_server_info == null)
		{
			return;
		}
		
		MxNetworkManager.GetInstance().CloseChannel(m_connect_game_server_info.HandleChannel);
		
		m_connect_game_server_info = null;
	}
	
	void OnEventDisconnectGameServer()
	{
		//DisconnectGameServer();
		
		m_connect_game_server_info = null;
		
		if (OnDisconnectGameServerConfirm != null)
		{
			OnDisconnectGameServerConfirm();
		}
	}
	
	/*
	public void SubmitValidateLoginToken(string token)
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.ValidateLoginToken);
        Req.Write(token);
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseValidateLoginToken(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		AccountInfo.AccountLoginGameServer(pkt.ReadUInt8() != 0);
	}
	
	public void SubmitCreateCharactor(string name, int sex, CharactorInfo.CharactorType crt_type)
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.CreateCharactor);
		
        Req.Write(name);
		
		if (sex == 0)
		{
			switch (crt_type)
			{
			case CharactorInfo.CharactorType.Jianke: Req.Write((short)1); break;
			case CharactorInfo.CharactorType.Neijia: Req.Write((short)3); break;
			case CharactorInfo.CharactorType.Yishi: Req.Write((short)5); break;
			}
		}
		else if (sex == 1)
		{
			switch (crt_type)
			{
			case CharactorInfo.CharactorType.Jianke: Req.Write((short)2); break;
			case CharactorInfo.CharactorType.Neijia: Req.Write((short)4); break;
			case CharactorInfo.CharactorType.Yishi: Req.Write((short)6); break;
			}
		}
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseCreateCharactor(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		CharactorInfo.CharactorCreate();
	}
	
	
	public void SubmitGetCharactorInfo()
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.GetCharactorInfo);
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	
	void OnParseGetCharactorInfo(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		CharactorInfo.Info info = new CharactorInfo.Info();
		
		info.ID = pkt.ReadInt32();
		info.Name = pkt.ReadString();
		info.CrtType = (CharactorInfo.CharactorType)pkt.ReadInt8();
		info.HeroID = pkt.ReadInt32();
		info.Rank = pkt.ReadInt32();
		info.Credit = pkt.ReadInt32();
		info.Thew = pkt.ReadInt16();
		info.ThewSec = pkt.ReadInt16();
		info.Energy = pkt.ReadInt16();
		info.EnergySec = pkt.ReadInt16();
		info.Friendship = pkt.ReadInt32();
		info.GP = pkt.ReadInt32();
		info.CP = pkt.ReadInt32();
		info.Grade = pkt.ReadInt8();
		info.Exp = pkt.ReadInt64();
		info//MxNetworkManager.GetInstance ().CloseChannel (pkt.GetPacketChannelID ());Exp = pkt.ReadInt64();
		info.MapID = pkt.ReadInt16();
				
		info.Partners = new List<CharactorInfo.Partner>();
		
		int num = pkt.ReadInt32();
		
		for (int i = 0; i < num; ++ i)
		{
			CharactorInfo.Partner ptn = new CharactorInfo.Partner();
			
			ptn.HeroInstID = pkt.ReadInt32();
			ptn.HeroID = pkt.ReadUInt16();
			ptn.StarGrade = pkt.ReadUInt8();
			
			info.Partners.Add(ptn);
		}
		
		CharactorInfo.Infos = info;
	}
	
	
	
	public void SubmitGetGroupInfo()
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.GetGroupInfo);
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseGetGroupInfo(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		GroupInfo.Info info = new GroupInfo.Info();
		
		info.Groups = new List<GroupInfo.Group>();
		
		int num = pkt.ReadInt32();
		
		for (int i = 0; i < num; ++ i)
		{
			GroupInfo.Group g = new GroupInfo.Group();
			
			g.ID = pkt.ReadUInt16();
			g.Type = pkt.ReadUInt8();
			
			g.Members = new List<int>();
			for (int j = 0; j < 6; ++j)
			{
				g.Members.Add(pkt.ReadInt32());
			}
						
			info.Groups.Add(g);
		}
						
		GroupInfo.Infos = info;
	}
	
	
	public void SubmitSetGroupMember(int id, List<int> members)
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.SetGroupMember);
		
		Req.Write((short)id);
		for (int i = 0; i < members.Count; ++ i)
		{
			Req.Write(members[i]);
		}
		
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseSetGroupMember(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		SubmitGetGroupInfo();
	}
	
	
	public void SubmitGetAreaInfo()
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.GetAreaInfo);
		
		MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseGetAreaInfo(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		List<WorldInfo.AreaInfo> areas = new List<WorldInfo.AreaInfo>();
		
		int num = pkt.ReadInt32();
		
		for (int i = 0; i < num; ++ i)
		{
			WorldInfo.AreaInfo area = new WorldInfo.AreaInfo(WorldInfo.Instance);
			
			area.ID = pkt.ReadUInt16();
			area.Type = pkt.ReadUInt8();
			area.PrevID = pkt.ReadUInt16();
			area//MxNetworkManager.GetInstance ().CloseChannel (pkt.GetPacketChannelID ());ID = pkt.ReadUInt16();
			
			areas.Add(area);
		}
		
		WorldInfo.Instance.UpdateWorld(areas);
	}
	
	public void SubmitGetPlaceInfoByArea(int id)
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.GetPlaceInfoByArea);
		
		Req.Write((short)id);
				
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseGetPlaceInfoByArea(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		int area_id = pkt_rq.ReadUInt16();
		
		WorldInfo.AreaInfo ai = WorldInfo.Instance.FindArea(area_id);
		
		
		List<WorldInfo.PlaceInfo> places = new List<WorldInfo.PlaceInfo>();
		
		int num = pkt.ReadInt32();
		
		for (int i = 0; i < num; ++ i)
		{
			WorldInfo.PlaceInfo place = new WorldInfo.PlaceInfo(ai);
			
			place.ID = pkt.ReadUInt16();
			place.Type = pkt.ReadUInt8();
			place.PrevID = pkt.ReadUInt16();
			place//MxNetworkManager.GetInstance ().CloseChannel (pkt.GetPacketChannelID ());ID = pkt.ReadUInt16();
			
			places.Add(place);
		}
		
		WorldInfo.Instance.UpdateArea(area_id, places);
	}
	
	
	public void SubmitGetLevelInfoByPlace(int id)
	{
		if (m_connect_game_server_info == null)
			return;
		
		MxNwkPacket Req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
        
        Req.SetPacketID((uint)NetworkDefine.Message.GetLevelInfoByPlace);
		
		Req.Write((short)id);
				
        MxNetworkManager.GetInstance().SendRequest(m_connect_game_server_info.HandleChannel, Req);
	}
	void OnParseGetLevelInfoByPlace(MxNwkPacket pkt, MxNwkPacket pkt_rq)
	{
		int place_id = pkt_rq.ReadUInt16();
		
		WorldInfo.PlaceInfo pi = WorldInfo.Instance.FindPlace(place_id);
		
		
		List<WorldInfo.LevelInfo> levels = new List<WorldInfo.LevelInfo>();
		
		int num = pkt.ReadInt32();
		
		for (int i = 0; i < num; ++ i)
		{
			WorldInfo.LevelInfo level = new WorldInfo.LevelInfo(pi);
			
			level.ID = pkt.ReadUInt16();
			level.Type = pkt.ReadUInt8();
			level.StarGrade = pkt.ReadUInt8();
			int map = pkt.ReadUInt16();
			int place = pkt.ReadUInt16();
			int sence = pkt.ReadUInt8();
			level.MinRank = pkt.ReadUInt8();
			level.LimitHeroID = pkt.ReadUInt16();
			level.NPC_ID = pkt.ReadUInt16();
			level.SpendThew = pkt.ReadInt16();
			level.PassCountLimit = pkt.ReadUInt8();
			level//MxNetworkManager.GetInstance ().CloseChannel (pkt.GetPacketChannelID ());ID = pkt.ReadUInt16();
			level.DialogID = pkt.ReadInt32();
			level.EnemyGroupID = pkt.ReadUInt16();
			level.RewardEXP = (int)pkt.ReadInt64();
			level.RewardGP = pkt.ReadInt32();
			
			for (int item = 0; item < 3; ++ item)
			{
				int item_id = pkt.ReadUInt16();
				int item_type = pkt.ReadUInt8();
				
				level.RewardItems.Add(item_id);
			}
			
			level.IsPassed = pkt.ReadUInt8() != 0;
			level.PassCount = pkt.ReadUInt8();
			
			levels.Add(level);
		}
						
		WorldInfo.Instance.UpdatePlace(place_id, levels);
	}*/
	
}

